/*
 * This file is part of LexiToolsMobile
 *  
 * Author: Jerzy Wawro
 * ver. 1.00
 * Copyright: Tenar (http://www.tenar.pl)
 *
 * Mobile LexiTools is an open source project; you can redistribute it 
 * and/or modify it under the terms of the license as published -  GPL with 
 * the following modification:  fragments of the code may be not copyleft 
 * (hidden), to protect contents of the dicionaries.
 * In this case the author of non-public fragments of the code is obliged 
 * to make available functionally equivalent code for unprotected dictionaries.
 * 
 * Mobile LexiTools is distributed WITHOUT ANY WARRANTY.
 * See the license for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with LexiToolsMobile (see: gpl.txt); if not - you can find it 
 * on web page http://www.fsf.org/, or write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package dict;

import dict.pgfile.*;


public class cache {
/* pamiec   stron  */

/*---------------- parametry ---------------------*/
static int  CACHESIZE = 64;   /* ilosc stron w pamieci operacyjnej */
page[] pages;


public cache() {
/* konstruktor inicjuje bufory */
 int i;

 pages = new page[CACHESIZE];
 /* inicjowanie zarz�dcy pami�ci� stronicowan� */
 for (i = 0; i < CACHESIZE; i++)  {
     pages[i] = new page();
     pages[i].ref = (short)i;
 }
}

page getpage( pgfile owner, short pageref)
/* Pobranie strony */
/* owner - w�a�ciciel (plik) */
/* pageref - numer strony */
{
  int i,j;
  page pg;

  i=0; pg=null;
  while ((i<CACHESIZE) && (pg==null)) {
      if ( (pages[i].owner == owner) && 
           (pages[i].ref == pageref)) {
          pg=pages[i];
          for (j=i;  j>0; j--) {
              pages[j]=pages[j-1];
          }
          pages[0]=pg;
      }
      else { 
          i++;
      }
  }
  if (pg==null) {
      j=(int)((pageref-1) / pgfile.PAGES_PER_FILE);
      owner.read(j+1, pages, CACHESIZE - pgfile.PAGES_PER_FILE);
      j = j * pgfile.PAGES_PER_FILE;
      for (i=CACHESIZE - pgfile.PAGES_PER_FILE; i<CACHESIZE; i++) 
      {
          pages[i].owner=owner;
          j++;
          pages[i].ref=(short)j;
          if ((short)j==pageref) { 
              pg=pages[i]; 
          }
      }
  }
  return (pg);

}



}
